package com.io.typescript.codegen.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EnumUtil {
    private static final String TAG = "**************************************";

    public static Map<String, String> parseEnum(String desc) {
        Map<String, String> map = new HashMap<>();
        if (desc.contains("=") && desc.contains("<br />")) {
            String[] arr = desc.split("<br />");
            for (String s : arr) {
                if (s.contains("=")) {
                    String[] names = s.split("=");
                    if (names.length == 2) {
                        map.put(names[1].trim(), toEnumName(names[0]));
                        System.out.println(TAG+names[1].trim()+","+toEnumName(names[0]));
                    }
                }
            }
        }
        return map;
    }

    public static String toEnumName(String name) {
        StringBuilder enmuName = new StringBuilder();
        char[] chars = name.trim().toCharArray();
        if (chars.length == 0) {
            return "";
        }
        int index = 0;
        for (int i = chars.length - 1; i >= 0; i--) {
            if (!isLetter(chars[i])) {
                index = i + 1;
                break;
            }
        }
        for (int i = index; i < chars.length; i++) {
            enmuName.append(chars[i]);
        }
        return enmuName.toString();
    }

    public static boolean isLetter(Character c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }

    /**
     * 字符串是否包含字母
     *
     * @param name 字符串
     * @return 只要包含一个就返回true
     */
    public static boolean hasLetter(String name) {
        char[] chars = name.trim().toCharArray();
        for (char c : chars) {
            if (isLetter(c)) {
                return true;
            }
        }
        return false;
    }

    public static boolean descHasAllValue(String desc, List<Object> values) {
        if (desc == null || desc.equals("")) {
            return false;
        }
        Map<String,String> map=parseEnum(desc);
        for(String key:map.keySet()){
            if(StringUtils.isEmpty(key)||StringUtils.isEmpty(map.get(key))){
                return false;
            }
        }
        boolean hasAll = true;
        for (Object value : values) {
            if (!desc.contains(value.toString())) {
                hasAll = false;
                break;
            }
        }
        return hasAll;
    }
}
